/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.istia.gesstages.util;

import fr.istia.gesstages.exception.PortailException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author usrlocal
 */
public class Encryption {
    
    public static String md5(String rawString){
        try {
            byte[] uniqueKey = rawString.getBytes();
            byte[] hash = MessageDigest.getInstance("MD5").digest(uniqueKey);
            StringBuilder hashString = new StringBuilder();
            for(int i = 0; i < hash.length; i++){
                String hex = Integer.toHexString(hash[i]);
                if(hex.length() == 1){
                    hashString.append('0');
                    hashString.append(hex.charAt(hex.length() - 1));
                }
                else{
                    hashString.append(hex.substring(hex.length() - 2));
                }
            }
            return hashString.toString();
        } catch (NoSuchAlgorithmException ex) {
            throw new PortailException("Erreur lors du cryptage du mot de passe", ex);
        }
    }
}
